﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Principal;
using DoeObjects;

namespace DoeLibsMVC.Security
{
    /// <summary>
    /// This is the class contain the logged in user. It will be bound to the HTTPContext and be available in each controller and view
    /// </summary>
    public class CustomPrincipal : DoeObjects.User, IPrincipal
    {
        public IIdentity Identity { get; private set; }
        public bool IsInRole(string role)
        {
            if (string.IsNullOrEmpty(role) || role == base.Category.Name)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public CustomPrincipal(int userId)
        {
            this.Identity = new GenericIdentity(userId.ToString());
            
            User user = User.getUserByUserId(userId);
            if (user != null)
            {
                base.Category = user.Category;
                base.FirstName = user.FirstName;
                base.LastName = user.LastName;
                base.Password = user.Password;
                base.Salt = user.Salt;
                base.UserId = user.UserId;
                base.EMail = user.EMail;
                base.Room = user.Room;
                base.UniPhone = user.UniPhone;
                base.Activated = user.Activated;
                base.ExpireNotificationSent = user.ExpireNotificationSent;
                base.RegistrationDate = user.RegistrationDate;
                base.UserExpirationDate = user.UserExpirationDate;
            }
        }
    }
}